/* proxydroid - Global / Individual Proxy App for Android
 * Copyright (C) 2011 K's Maze <kafkasmaze@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package proxydroid.org.proxydroid;


import com.google.gson.annotations.Expose;

import java.io.Serializable;
import java.net.InetAddress;
import java.util.Vector;
import java.util.regex.Pattern;

/**
 * @author KsMaze
 * 
 */
public class Profile extends GsonSerializable implements Serializable, Cloneable{
	@Expose
	private String name;
	@Expose
	private String host;
	@Expose
	private String proxyType;
	@Expose
	private int port;
	@Expose
	private String bypassAddrs;
	@Expose
	private String user;
	@Expose
	private String password;
	//如果为全局则proxyedApps，为空。isBypassApps为false
	//如果false，则proxyedApps中的app设置为代理，其他不设置为代理
	//如果true，则proxyedApps中的app不设置代理，其他设置为代理
	//此项目中proxyedApps只有一个，isBypassApps固定为false
	@Expose
	private String proxyedApp;
	@Expose
	private boolean isBypassApps = false;
	@Expose
	private boolean isAutoConnect = false;
	@Expose
	private boolean isAutoSetProxy = false;
	@Expose
	private boolean isAuth = false;
	@Expose
	private boolean isNTLM = false;
	@Expose
	private boolean isDNSProxy = false;
	@Expose
	private boolean isPAC = false;
	@Expose
	private String domain;
	@Expose
	private String ssid;
	//可执行文件DIR
	@Expose
	private String exeFilePath;
	//redsocks相关配置文件，PID文件存放位置
	@Expose
	private String pluginWorkPath;
	public Profile() {
		init();
	}
	public void init() {
		host = "";
		port = 3128;
		ssid = "";
		user = "";
		domain = "";
		password = "";
		isAuth = false;
		proxyType = "http";
		isAutoConnect = false;
		ssid = "";
		isNTLM = false;
		bypassAddrs = "";
		proxyedApp = "";
		isDNSProxy = false;
		isPAC = false;
	}

	//对象克隆复制
	@Override
	public Object clone() {
		Profile profile = null;
		try{
			profile = (Profile)super.clone();
		}catch(CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return profile;
	}

	public String toString() {
		return this.toJson();
	}

	public static String validateAddr(String ia) {
		boolean valid1 = Pattern.matches(
				"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}/[0-9]{1,2}",
				ia);
		boolean valid2 = Pattern.matches(
				"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}", ia);

		if (valid1 || valid2) {

			return ia;

		} else {
			String addrString = null;

			try {
				InetAddress addr = InetAddress.getByName(ia);
				addrString = addr.getHostAddress();
			} catch (Exception ignore) {
				addrString = null;
			}

			if (addrString != null) {
				boolean valid3 = Pattern.matches(
						"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}",
						addrString);
				if (!valid3)
					addrString = null;
			}

			return addrString;
		}
	}

	public static String[] decodeAddrs(String addrs) {
		String[] list = addrs.split("\\|");
		Vector<String> ret = new Vector<String>();
		for (String addr : list) {
			String ta = validateAddr(addr);
			if (ta != null)
				ret.add(ta);
		}
		return ret.toArray(new String[ret.size()]);
	}

	public static String encodeAddrs(String[]addrs) {

		if (addrs.length == 0)
			return "";

		StringBuffer sb = new StringBuffer();
		for (String addr : addrs) {
			String ta = validateAddr(addr);
			if (ta != null)
				sb.append(ta + "|");
		}
		String ret = sb.substring(0, sb.length() - 1);
		return ret;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the ssid
	 */
	public String getSsid() {
		return ssid;
	}

	/**
	 * @param ssid
	 *            the ssid to set
	 */
	public void setSsid(String ssid) {
		this.ssid = ssid;
	}

	/**
	 * @return the host
	 */
	public String getHost() {
		return host;
	}

	/**
	 * @param host
	 *            the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * @return the proxyType
	 */
	public String getProxyType() {
		return proxyType;
	}

	/**
	 * @param proxyType
	 *            the proxyType to set
	 */
	public void setProxyType(String proxyType) {
		this.proxyType = proxyType;
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return port;
	}

	/**
	 * @param port
	 *            the port to set
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/**
	 * @return the bypassAddrs
	 */
	public String getBypassAddrs() {
		return bypassAddrs;
	}

	/**
	 * @param bypassAddrs
	 *            the bypassAddrs to set
	 */
	public void setBypassAddrs(String bypassAddrs) {
		this.bypassAddrs = bypassAddrs;
	}

	/**
	 * @return the user
	 */
	public String getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(String user) {
		this.user = user;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the isAutoConnect
	 */
	public Boolean isAutoConnect() {
		return isAutoConnect;
	}

	/**
	 * @param isAutoConnect
	 *            the isAutoConnect to set
	 */
	public void setAutoConnect(Boolean isAutoConnect) {
		this.isAutoConnect = isAutoConnect;
	}

	/**
	 * @return the isAutoSetProxy
	 */
	public Boolean isAutoSetProxy() {
		return isAutoSetProxy;
	}

	/**
	 * @param isAutoSetProxy
	 *            the isAutoSetProxy to set
	 */
	public void setAutoSetProxy(Boolean isAutoSetProxy) {
		this.isAutoSetProxy = isAutoSetProxy;
	}

	/**
	 * @return the isBypassApps
	 */
	public Boolean isBypassApps() {
		return isBypassApps;
	}

	/**
	 * @param isBypassApps
	 *            the isBypassApps to set
	 */
	public void setBypassApps(Boolean isBypassApps) {
		this.isBypassApps = isBypassApps;
	}

	/**
	 * @return the isAuth
	 */
	public Boolean isAuth() {
		return isAuth;
	}

	/**
	 * @param isAuth
	 *            the isAuth to set
	 */
	public void setAuth(Boolean isAuth) {
		this.isAuth = isAuth;
	}

	/**
	 * @return the isNTLM
	 */
	public Boolean isNTLM() {
		return isNTLM;
	}

	/**
	 * @param isNTLM
	 *            the isNTLM to set
	 */
	public void setNTLM(Boolean isNTLM) {
		this.isNTLM = isNTLM;
	}

	/**
	 * @return the domain
	 */
	public String getDomain() {
		return domain;
	}

	/**
	 * @param domain
	 *            the domain to set
	 */
	public void setDomain(String domain) {
		this.domain = domain;
	}

	/**
	 * @return the isDNSProxy
	 */
	public boolean isDNSProxy() {
		return isDNSProxy;
	}

	/**
	 * @param isDNSProxy
	 *            the isDNSProxy to set
	 */
	public void setDNSProxy(boolean isDNSProxy) {
		this.isDNSProxy = isDNSProxy;
	}

	/**
	 * @return the isPAC
	 */
	public boolean isPAC() {
		return isPAC;
	}

	/**
	 * @param isDNSProxy
	 *            the isDNSProxy to set
	 */
	public void setPAC(boolean isPAC) {
		this.isPAC = isPAC;
	}

	public String getProxyedApp() {
		return proxyedApp;
	}

	public void setProxyedApp(String proxyedApps) {
		this.proxyedApp = proxyedApps;
	}

	public void setBypassApps(boolean bypassApps) {
		isBypassApps = bypassApps;
	}

	public void setAutoConnect(boolean autoConnect) {
		isAutoConnect = autoConnect;
	}

	public void setAutoSetProxy(boolean autoSetProxy) {
		isAutoSetProxy = autoSetProxy;
	}

	public void setAuth(boolean auth) {
		isAuth = auth;
	}

	public void setNTLM(boolean NTLM) {
		isNTLM = NTLM;
	}
}
